/**
 *  program search 
 *  search the index file
 *  output the result
 *
 *  Date:2008-11-20
 *  Author:Wang Lijun
 *  Email:unix_jun@yahoo.com.cn
 */

#include <iostream>
#include <string>

#include <unistd.h>
#include <getopt.h>

#include "page_vector.h"
#include "query_vector.h"
#include "page_ranking.h"
#include "output.h"
#include "page_db.h"
#include "flat_output.h"

using namespace jsearch;

std::string program_name;

struct option long_options[]=
{
	{"db",required_argument,NULL,'d'},
	{"page_db",required_argument,NULL,'p'},
	{"verbose",required_argument,NULL,'b'},
	{"version",no_argument,NULL,'v'},
	{"help",no_argument,NULL,'h'}
};

void usage(int status)
{
	if (status)	{
		std::cerr<<"Try "<<program_name
			<<" --help for more infomation"
			<<std::endl;
	} else {
		std::cout<<"Usage:"
			<<program_name
			<<" [OPTIONS] terms"<<std::endl<<std::endl
			<<"  --db      -d file Where the indexed data to store"<<std::endl
			<<"  --page_db -p file File to store page_db"<<std::endl
			<<"  --verbose -v [on][off] Turn on or turn off the verbose mode"
			<<std::endl
			<<"  --version -V      Show version"<<std::endl
			<<"  --help    -h      Show help infomation"<<std::endl
			<<std::endl
			<<"Bug Report<unix_jun@yahoo.com.cn>"<<std::endl;
	}
}

void show_version()
{
	std::cout<<"simple search 0.01"<<std::endl
		<<"Author: Wang Li Jun"<<std::endl;
}

int main(int argc, char **argv)
{
	program_name = argv[0];

	std::string disk_db="rindex.db";
	std::string page_db_file="PAGE.db";
	bool verbose = true;

	char c;
	while ( (c=getopt_long(argc, argv,"d:p:v:Vh",long_options,0)) != EOF)
	{
		switch (c)
		{
			case 'd':
				disk_db = std::string(optarg);
				break;
			case 'p':
				page_db_file = std::string(optarg);
				break;
			case 'v':
				if (std::string(optarg) == "on")
					verbose = true;
				else if (std::string(optarg) == "off")
					verbose = false;
				else {
					std::cout<<"verbose argument should be between on and off"
						<<std::endl;
					exit(1);
				}
				break;
			case 'V':
				show_version();
				exit(0);
			case 'h':
				usage(0);
				exit(0);
			default:
				usage(1);
				exit(1);
				break;
		}
	}

	if (optind >= argc)	{
		usage(1);
		exit(1);
	}

	std::tr1::shared_ptr< std::vector<std::string> > \
		querys(new std::vector<std::string>);

	for (int i=optind;i<argc;i++)
		querys->push_back(argv[i]);


	std::tr1::shared_ptr<jsearch::page_vector> \
		page_vectors(new jsearch::page_vector(disk_db));

	page_vectors->restore(querys);

	std::tr1::shared_ptr<jsearch::query_vector> \
		query_vectors(new jsearch::query_vector(querys));
	query_vectors->query_build();

	jsearch::page_ranking ranker (page_vectors, query_vectors);
	ranker.ranking();

	std::vector<jsearch::prio_page> prio_pg=ranker.get_prio_pages();

	std::tr1::shared_ptr<jsearch::page_db> \
	   	pdb(new jsearch::page_db("",page_db_file)); 
	pdb->restore();

	std::tr1::shared_ptr<jsearch::output> \
		print(new jsearch::flat_output(pdb, querys,verbose));
	print->put(prio_pg);

	return 0;
}
